layui.use(['jquery', 'element', 'layer', 'form', 'layedit'], function () {
    var $ = layui.$;
    var layedit = layui.layedit;
    var form = layui.form;
    var ctxPath = "/education";


    var initCustomer = function (id) {
        $.ajax({
            url: ctxPath + '/api/schoolGradeClass/listSelectOptions',
            type: 'post',
            dataType: 'json',
            success: function (result) {
                if (result.code == 0) {
                    var data = result.data;
                    if (data != null) {
                        var select = $("#schoolGradeClassId");
                        for (var i = 0; i < data.length; i++) {
                            var sgc = data[i];
                            select.append('<option value="'+sgc.id+'" >'+sgc.name+'</option>');
                        }
                    }
                    form.render();
                } else {
                    layer.alert("get data fail：" + result.msg);
                }
            }
        });


        if (!id) {
            return;
        }

        $("#pwdDiv").html("");
        $.ajax({
            url: ctxPath + '/api/customer/findById',
            type: 'post',
            dataType: 'json',
            data: {id: id},
            success: function (result) {
                if (result.code == 0) {
                    var data = result.data;
                    if (data != null) {
                        $("input[name='id']").val(data.id);
                        $("input[name='code']").val(data.code);
                        $("input[name='name']").val(data.name);
                        $("input[name='phone']").val(data.phone);
                        $("input[name='point']").val(data.point);
                        $("#enabled").val(data.enabled);
                        $("#type").val(data.type);
                        $("#schoolGradeClassId").val(data.schoolGradeClassId);
                    }
                } else {
                    layer.alert("get data fail：" + result.msg);
                }
            }
        });
    }

    initCustomer(window.location.search.substring(4));

    $("#saveBtn").click(function () {
        var request = {
            id: $("input[name='id']").val(),
            code: $("input[name='code']").val(),
            name: $("input[name='name']").val(),
            phone: $("input[name='phone']").val(),
            password: $("input[name='password']").val(),
            point: $("input[name='point']").val(),
            schoolGradeClassId : $("#schoolGradeClassId").val(),
            enabled: $("#enabled").val(),
            type: $("#type").val()
        };

        var url = ctxPath + "/api/customer/modify";
        if (!request.id) {
            url = ctxPath + "/api/customer/create";
        }

        $.ajax({
            url: url,
            data: JSON.stringify(request),
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            success: function (result) {
                if (result.code == 0) {
                    layer.alert('保存成功！', function (index) {
                        layer.close(index);
                        location.href = ctxPath + '/html/customer/customerList.html';
                    });
                } else {
                    layer.alert("保存失败：" + result.msg);
                }
            }
        });
    });

    $("#cancelBtn").click(function() {
        location.href = ctxPath + '/html/customer/customerList.html';
    });
});

